package com.codeczx.completeuidemo.recyclerview.md;

import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.Adapter;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.codeczx.completeuidemo.R;
import com.codeczx.completeuidemo.recyclerview.library.sticky_headers.*;

import java.util.ArrayList;
import java.util.List;

public class RV1Adapter extends Adapter<ViewHolder> implements StickyHeaders, StickyHeaders.ViewSetup {
    private static final String TAG = "RV1Adapter";
    private static final int VIEW_TYPE_TYPE1 = 0;
    private static final int VIEW_TYPE_TYPE2 = 1;
    private static final int VIEW_TYPE_SEPARATOR_SPACER = 2;

    private List<Object> items;
    private Callback mCallback;

    public RV1Adapter(Callback callback) {
        mCallback = callback;
    }

    public void setItems(List<Object> items) {
        List<Object> newData = new ArrayList<>();
        for (int i = 0; i < items.size(); i++) {
            Object item = items.get(i);
            newData.add(item);
            if (item instanceof Type2Bean && i + 1 < items.size() && items.get(i + 1) instanceof Type2Bean) {
                newData.add(new SeparatorSpacer());
            }
        }
        this.items = newData;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case VIEW_TYPE_TYPE1:
                return Type1ViewHolder.newInstance(parent, mCallback);
            case VIEW_TYPE_TYPE2:
                return Type2ViewHolder.newInstance(parent, mCallback);
            case VIEW_TYPE_SEPARATOR_SPACER:
                return SeparatorSpacerViewHolder.newInstance(parent);
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Object item = items.get(position);
        switch (holder.getItemViewType()) {
            case VIEW_TYPE_TYPE1:
                ((Type1ViewHolder) holder).bind((Type1Bean) item);
                break;
            case VIEW_TYPE_TYPE2:
                ((Type2ViewHolder) holder).bind((Type2Bean) item);
                break;
            case VIEW_TYPE_SEPARATOR_SPACER:
                break;
        }
    }

    @Override
    public int getItemViewType(int position) {
        if (items.get(position) instanceof Type1Bean) {
            return VIEW_TYPE_TYPE1;
        }
        if (items.get(position) instanceof Type2Bean) {
            return VIEW_TYPE_TYPE2;
        }
        if (items.get(position) instanceof SeparatorSpacer) {
            return VIEW_TYPE_SEPARATOR_SPACER;
        }
        return RecyclerView.INVALID_TYPE;
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }

    @Override
    public boolean isStickyHeader(int position) {
        return getItemViewType(position) == VIEW_TYPE_TYPE1;
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void setupStickyHeaderView(View stickyHeader) {
        stickyHeader.setTranslationZ(4.0f);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    public void teardownStickyHeaderView(View stickyHeader) {
        stickyHeader.setTranslationZ(0f);
    }

    private static class Type1ViewHolder extends ViewHolder {

        private final TextView tvTitle;
        private final Callback mCallback;

        public static Type1ViewHolder newInstance(ViewGroup parent, Callback callback) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv1_type1, parent, false);
            return new Type1ViewHolder(itemView, callback);
        }

        private Type1ViewHolder(View itemView, Callback callback) {
            super(itemView);
            tvTitle = (TextView) itemView.findViewById(R.id.tv_type1);
            mCallback = callback;
        }

        public void bind(final Type1Bean type1Bean) {
            tvTitle.setText(type1Bean.getContent());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallback.onType1Click(type1Bean.getContent());
                }
            });
        }
    }

    private static class Type2ViewHolder extends ViewHolder {

        private final TextView tvContent;
        private final Callback mCallback;

        public static Type2ViewHolder newInstance(ViewGroup parent, Callback callback) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_rv1_type2, parent, false);
            return new Type2ViewHolder(itemView, callback);
        }

        private Type2ViewHolder(View itemView, Callback callback) {
            super(itemView);
            tvContent = (TextView) itemView.findViewById(R.id.tv_type2);
            mCallback = callback;
        }

        public void bind(final Type2Bean type2Bean) {
            tvContent.setText(type2Bean.getContent());
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallback.onType2Click(type2Bean.getContent());
                }
            });
        }
    }

    private static class SeparatorSpacerViewHolder extends ViewHolder {

        public static SeparatorSpacerViewHolder newInstance(ViewGroup parent) {
            View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_spacer, parent, false);
            return new SeparatorSpacerViewHolder(itemView);
        }

        private SeparatorSpacerViewHolder(View itemView) {
            super(itemView);
        }
    }

    private static class SeparatorSpacer {
    }

    public interface Callback {
        void onType1Click(Object item);

        void onType2Click(Object item);
    }
}
